//
//  AlertView.m
//  LocomotiveGuards
//
//  Created by SmileFish on 16/7/5.
//  Copyright © 2016年 REDOOR. All rights reserved.
//

#import "AlertView.h"

#define TIPVIEW_WIDTH               0.8*SCREEN_WIDTH    //提示框宽度
#define TIPVIEW_TITLELABEL_HEIGHT   0.05*SCREEN_HEIGHT + 10  //标题高度
#define TIPVIEW_BUTTON_HEIGHT       0.04*SCREEN_HEIGHT  //按钮高度
#define TIPVIEW_BUTTON_WIDTH        0.36*TIPVIEW_WIDTH  //按钮宽度
#define TIPVIEW_MESSAGE_FONT_SIZE   12                  //提示内容字体大小
#define TIPVIEW_CORNER_RADIUS       5                   //提示框视图圆角大小
#define TIPVIEW_MESSAGE_INTVAL      10                  //提示内容上下边距
@interface AlertView()

@property (nonatomic,copy) AlertViewBlockType confirmButtonBlock;
@property (nonatomic,copy) AlertViewBlockType cancelButtonBlock;

@end

@implementation AlertView

#pragma mark --init--

- (id)initWithConfirmButtonBlock:(AlertViewBlockType)confirmBlock
               cancelButtonBlock:(AlertViewBlockType)cancelBlock
{
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]){
        
        //创建标题和内容体
        UIView * tipView = [self viewWithMessage:@"当前处于游客模式，请先注册账号"];
        [self addSubview:tipView];
        
        //取消按钮
        UIButton * cancelButton = [[UIButton alloc]init];
        cancelButton.frame = CGRectMake(0, tipView.frame.size.height - 40, tipView.frame.size.width / 2, 40);
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:RGBACOLOR(56, 139, 245, 1.0) forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        //确认按钮
        UIButton * confirmButton = [[UIButton alloc]init];
        confirmButton.frame = CGRectMake(tipView.frame.size.width / 2,  tipView.frame.size.height - 40, tipView.frame.size.width / 2, 40);
        confirmButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
        [confirmButton setTitle:@"注册" forState:UIControlStateNormal];
        [confirmButton setTitleColor:RGBACOLOR(56, 139, 245, 1.0) forState:UIControlStateNormal];
        [confirmButton addTarget:self action:@selector(confirmButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        [tipView addSubview:confirmButton];
        [tipView addSubview:cancelButton];
        
        self.confirmButtonBlock = confirmBlock;
        self.cancelButtonBlock  = cancelBlock;
    }
    
    return self;
}

#pragma mark --custom function--

- (UIView *)viewWithMessage:(NSString *)message
{
    //背景置灰
    UIView * backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    backGroundView.backgroundColor = RGBACOLOR(21, 21, 21,0.5);
    [self addSubview:backGroundView];
    
    CGFloat tipViewHeight = TIPVIEW_TITLELABEL_HEIGHT;
    CGFloat messageHeight = [self calculateNSStringSize:message font:[UIFont systemFontOfSize:TIPVIEW_MESSAGE_FONT_SIZE] maxWidth:TIPVIEW_WIDTH*0.8];
    tipViewHeight += messageHeight;
    tipViewHeight += 3*TIPVIEW_MESSAGE_INTVAL + TIPVIEW_BUTTON_HEIGHT + 50;
    
    //提示框
    UIView * tipView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, TIPVIEW_WIDTH, tipViewHeight)];
    tipView.backgroundColor = [UIColor whiteColor];
    tipView.layer.cornerRadius = TIPVIEW_CORNER_RADIUS;
    tipView.center = CGPointMake(0.5*SCREEN_WIDTH, 0.5*SCREEN_HEIGHT);
    
    //分割线
    UIView * line = [[UIView alloc]init];
    line.frame = CGRectMake(0, tipView.frame.size.height - 40, TIPVIEW_WIDTH, 1);
    line.backgroundColor = RGBACOLOR(201, 201, 201, 1.0);
    [tipView addSubview:line];
    
    UIView *btnLine = [[UIView alloc]initWithFrame:CGRectMake(tipView.frame.size.width / 2 - 0.5, tipView.frame.size.height - 40, 1, 40)];
    btnLine.backgroundColor = RGBACOLOR(201, 201, 201, 1.0);
    [tipView addSubview:btnLine];
    
    //内容
    UILabel * messageLabel = [[UILabel alloc]init];
    messageLabel.frame = CGRectMake(10, TIPVIEW_TITLELABEL_HEIGHT + 10, TIPVIEW_WIDTH - 20, messageHeight+0.5*TIPVIEW_MESSAGE_INTVAL);
    messageLabel.font = [UIFont systemFontOfSize:15.0];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    messageLabel.text = message;
    messageLabel.textColor = [UIColor lightGrayColor];
    messageLabel.numberOfLines = 0;
    [tipView addSubview:messageLabel];
    
    return tipView;
}

#pragma mark --confirm and cancel button clicked handler--

- (void)confirmButtonClicked:(UIButton *) sender
{
    NSLog(@"---confirmButtonClicked---");
    if (self.confirmButtonBlock) {
        self.confirmButtonBlock();
    }
 
     [self removeFromSuperview];
}

- (void)cancelButtonClicked:(UIButton *) sender
{
    NSLog(@"---cancelButtonClicked---");
    if (self.cancelButtonBlock) {
        self.cancelButtonBlock();
    }
    
    [self removeFromSuperview];
}


/**
 *  获取字符串高度
 *
 *  @param text   字符串
 *  @param font   字体
 *  @param width 宽度限制
 *
 *  @return 字符串高度
 */
- (CGFloat)calculateNSStringSize:(NSString * )text font:(UIFont *) font maxWidth:(CGFloat) width
{
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    NSDictionary * attributes = @{NSFontAttributeName : font,
                                  NSParagraphStyleAttributeName : paragraphStyle};
    CGSize contentSize =
    [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                       options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                    attributes:attributes
                       context:nil]
    .size;
    return contentSize.height;
}

-(void)dealloc
{
    NSLog(@"-----dealloc:%@-----",NSStringFromClass([self class]));
}


@end
